<template>
    <div class="addressList">
        <van-cell-group >
            <van-nav-bar title="地址管理" left-arrow @click-left="back"/>
            <van-address-list v-model="chosenAddressId" :list="list" @add="onAdd" @edit="onEdit" />
        </van-cell-group>
        <van-popup v-model="update" position="right" class="v-full" :overlay="false">
            <Update ref="update" :popup="true" :callback="closeUpdate"></Update>
        </van-popup>
    </div>
</template>
<script>
import { CellGroup,NavBar,AddressList,Popup} from 'vant';

import Update from 'touch/components/home/service/location/update';

export default {
	props: {
        callback:{
            type: Function,
        },
        popup :{
        	type : Boolean,
        	default : false
        }
    },
    components: {
    	[CellGroup.name] : CellGroup,
    	[NavBar.name] : NavBar,
        [Popup.name] : Popup,
        [AddressList.name] : AddressList,
        Update,
    },
    data () {
        return {
            chosenAddressId: '0',
            list: [],
            update : false,
        }
    },
    methods :{
        init(){
            this.Axios.Home.Address.GetList()
            .then(res =>{
                if(!res) return false;
                var data = res.data;
                for(var i in data){
                    if(data[i]['isDefault']==1){
                        this.chosenAddressId = data[i]['id'];
                        break;
                    }
                }
                this.list = data;
            })
        },
        onAdd() {
            this.update = true;
            this.$nextTick(function () {
                this.$refs.update.init(0);
            })
        },
        onEdit(item, index) {
            this.update = true;
            this.$nextTick(function () {
                this.$refs.update.init(item.id);
            })
        },
        closeUpdate(){
            this.update = false;
            this.init();
        },
        back(){
        	if (this.popup) {
        		this.callback();
        	}else{
        		window.history.go(-1);
        	}
        },
    },
}
</script>
<style>
	.addressList .van-address-list {padding-bottom:0px;}
    .addressList .van-address-list__add{z-index: 1;}
</style>